package com.laiyima.zhongjiang.linghuilv.demo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class ModifyPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG="ModifyPasswordActivity";
    private int modifCode;
    private ImageView imageView;
    private EditText newPass,againpass;
    private Button modifOver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_password);
        imageView=findViewById(R.id.modify_back);
        newPass=findViewById(R.id.new_pass);
        againpass=findViewById(R.id.new_again_pass);
        modifOver=findViewById(R.id.modify_over);

        imageView.setOnClickListener(this);
        modifOver.setOnClickListener(this);

//        againpass.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                String s1=againpass.getText().toString();
//                String s2=newPass.getText().toString();
//                if(s1.equals(s2)){
//                    modifOver.setEnabled(true);
//                    modifOver.setTextColor(Color.WHITE);
//                    modifOver.setBackgroundResource(R.drawable.begin_textview_shape_change);
//                }else{
//                    modifOver.setEnabled(false);
//                    modifOver.setTextColor(Color.parseColor("#B8B8B8"));
//                    modifOver.setBackgroundResource(R.drawable.firist_edittext_shape);
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });

    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.modify_back:
                finish();
                break;
            case R.id.modify_over:
                Intent intent = getIntent();
                String number = intent.getStringExtra("number");
                String captch = intent.getStringExtra("captch");
                String password=newPass.getText().toString();
                String againPW=againpass.getText().toString();
                if (password.length()<6 || againPW.length()<6){
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入长度大于6位的密码", Toast.LENGTH_LONG).show();
                    return;
                } else if (password.equals(againPW)==false) {
                    Toast.makeText(this, "请两次输入一样密码", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/api/Register/edit_mobile_password");
                    //需要携带的参数
                    params.addBodyParameter("mobile", number);
                    params.addBodyParameter("code", captch);
                    params.addBodyParameter("new_password", password);
                    //  params.addBodyParameter("code", str);
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            try{
                                JSONObject jsonObject = new JSONObject(result);
                                modifCode = jsonObject.optInt("code", 0);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (modifCode==200){
                                Intent intent = new Intent(ModifyPasswordActivity.this, FirstActivity.class);
                                startActivity(intent);
                                Toast.makeText(getBaseContext().getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                            }
                            if (modifCode==201){

                                Toast.makeText(getBaseContext().getApplicationContext(), "输入不能为空", Toast.LENGTH_SHORT).show();
                            }
                            if (modifCode==204){

                                Toast.makeText(getBaseContext().getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                            }
                            if (modifCode==501){

                                Toast.makeText(getBaseContext().getApplicationContext(), "验证码错误", Toast.LENGTH_SHORT).show();
                            }
                            if(modifCode==202){
                                Toast.makeText(getBaseContext().getApplicationContext(), "密码长度至少 6 位，必须包含有大写字母、小写字母、数字", Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                        }

                        @Override
                        public void onCancelled(org.xutils.common.Callback.CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {
                        }

                    });



                }
                break;
        }
    }
}
